home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / EffectEdit / MainFrm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  11.3 KB  |  406 lines

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "EffectEdit.h"
  6.  
  7. #include "MainFrm.h"
  8. #include "EffectDoc.h"
  9. #include <D3D9.h>
  10. #include "UIElements.h"
  11. #include "RenderView.h"
  12. #include "OptionsView.h"
  13. #include "TextView.h"
  14. #include "ErrorsView.h"
  15.  
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CMySplitterWnd
  24.  
  25. void CMySplitterWnd::ActivateNext(BOOL bPrev)
  26. {
  27.     ASSERT_VALID(this);
  28.  
  29.     // find the coordinate of the current pane
  30.     int row, col;
  31.     if (GetActivePane(&row, &col) == NULL)
  32.     {
  33.         TRACE0("Warning: Cannot go to next pane - there is no current view.\n");
  34.         return;
  35.     }
  36.     ASSERT(row >= 0 && row < m_nRows);
  37.     ASSERT(col >= 0 && col < m_nCols);
  38.  
  39.     // determine next pane
  40.     if (bPrev)
  41.     {
  42.         // prev
  43.         if (--col < 0)
  44.         {
  45.             col = m_nCols - 1;
  46.             if (--row < 0)
  47.             {
  48.                 if( m_pSplitterWndPrev != NULL )
  49.                 {                               
  50.                     row = m_pSplitterWndPrev->GetRowCount() - 1;
  51.                     col = m_pSplitterWndPrev->GetColumnCount() - 1;
  52.                     m_pSplitterWndPrev->SetActivePane(row, col);
  53.                     CWnd* pWnd = m_pSplitterWndPrev->GetActivePane();
  54.                     if( pWnd->IsKindOf( RUNTIME_CLASS( CRenderView ) ) )
  55.                         m_pSplitterWndPrev->ActivateNext( bPrev );
  56.                     return;
  57.                 }
  58.                 row = m_nRows - 1;
  59.             }
  60.         }
  61.     }
  62.     else
  63.     {
  64.         // next
  65.         if (++col >= m_nCols)
  66.         {
  67.             col = 0;
  68.             if (++row >= m_nRows)
  69.             {
  70.                 if( m_pSplitterWndNext != NULL )
  71.                 {
  72.                     m_pSplitterWndNext->SetActivePane(0, 0);
  73.                     CWnd* pWnd = m_pSplitterWndPrev->GetActivePane();
  74.                     if( pWnd->IsKindOf( RUNTIME_CLASS( CRenderView ) ) )
  75.                         m_pSplitterWndPrev->ActivateNext( bPrev );
  76.                     return;
  77.                 }
  78.                 row = 0;
  79.             }
  80.         }
  81.     }
  82.  
  83.     // set newly active pane
  84.     SetActivePane(row, col);
  85.     CWnd* pWnd = GetActivePane();
  86.     if( pWnd->IsKindOf( RUNTIME_CLASS( CRenderView ) ) )
  87.         ActivateNext( bPrev );
  88. }
  89.  
  90.  
  91. void CMySplitterWnd::StopTracking(BOOL bAccept)
  92. {
  93.     m_bPreserveLastPaneSize = false;
  94.     CSplitterWnd::StopTracking(bAccept);
  95. }
  96.  
  97.  
  98. void CMySplitterWnd::RecalcLayout()
  99. {
  100.     CRect rectClient;
  101.     GetClientRect(rectClient);
  102.     rectClient.InflateRect(-m_cxBorder, -m_cyBorder);
  103.  
  104.     if( m_nCols > 1 )
  105.     {
  106.         // It's the horizontal splitter
  107.     }
  108.     else
  109.     {
  110.         // It's one of the vertical splitters
  111.         if( m_bPreserveLastPaneSize )
  112.         {
  113.             // Try to preserve the last row's current size
  114.             int nLastCurSize = m_pRowInfo[m_nRows - 1].nCurSize;
  115.             if( nLastCurSize != -1 )
  116.             {
  117.                 if( rectClient.Height() - nLastCurSize - m_cySplitter > m_pRowInfo[0].nMinSize )
  118.                     m_pRowInfo[0].nIdealSize = rectClient.Height() - nLastCurSize - m_cySplitter;
  119.             }
  120.             m_bPreserveLastPaneSize = false;
  121.         }
  122.     }
  123.     
  124.     CSplitterWnd::RecalcLayout();
  125. }
  126.  
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CMainFrame
  129.  
  130. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  131.  
  132. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  133.     //{{AFX_MSG_MAP(CMainFrame)
  134.     ON_WM_CREATE()
  135.     ON_COMMAND(ID_VIEW_RENDER, OnRender)
  136.     ON_COMMAND(ID_VIEW_CHANGEDEVICE, OnViewChangeDevice)
  137.     ON_WM_SIZE()
  138.     ON_WM_CLOSE()
  139.     ON_WM_ACTIVATEAPP()
  140.     //}}AFX_MSG_MAP
  141. END_MESSAGE_MAP()
  142.  
  143. static UINT indicators[] =
  144. {
  145.     ID_SEPARATOR,           // status line indicator
  146.     ID_INDICATOR_ROW,
  147.     ID_INDICATOR_COL,
  148.     ID_INDICATOR_CAPS,
  149.     ID_INDICATOR_NUM,
  150.     ID_INDICATOR_SCRL,
  151. };
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. // CMainFrame construction/destruction
  155.  
  156. CMainFrame::CMainFrame()
  157. {
  158. }
  159.  
  160. CMainFrame::~CMainFrame()
  161. {
  162. }
  163.  
  164. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  165. {
  166.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  167.         return -1;
  168.     if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
  169.         | /*CBRS_GRIPPER | */CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
  170.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  171.     {
  172.         TRACE0("Failed to create toolbar\n");
  173.         return -1;      // fail to create
  174.     }
  175.  
  176.     if (!m_wndStatusBar.Create(this) ||
  177.         !m_wndStatusBar.SetIndicators(indicators,
  178.           sizeof(indicators)/sizeof(UINT)))
  179.     {
  180.         TRACE0("Failed to create status bar\n");
  181.         return -1;      // fail to create
  182.     }
  183.  
  184.     // Set up the row and column pane width in the status bar
  185.     UINT nId, nStyle;
  186.     int    nWidth;
  187.     m_wndStatusBar.GetPaneInfo(1, nId, nStyle, nWidth);
  188.     nWidth = 60;
  189.     m_wndStatusBar.SetPaneInfo(1, nId, nStyle, nWidth);
  190.  
  191.     m_wndStatusBar.GetPaneInfo(2, nId, nStyle, nWidth);
  192.     nWidth = 60;
  193.     m_wndStatusBar.SetPaneInfo(2, nId, nStyle, nWidth);
  194.  
  195.     // Initialize the pane text to line 1 & col 1
  196.     SetRowCol(1, 1);
  197.  
  198.     return 0;
  199. }
  200.  
  201. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  202. {
  203.     if( !CFrameWnd::PreCreateWindow(cs) )
  204.         return FALSE;
  205.     // TODO: Modify the Window class or styles here by modifying
  206.     //  the CREATESTRUCT cs
  207.  
  208.     return TRUE;
  209. }
  210.  
  211. /////////////////////////////////////////////////////////////////////////////
  212. // CMainFrame diagnostics
  213.  
  214. #ifdef _DEBUG
  215. void CMainFrame::AssertValid() const
  216. {
  217.     CFrameWnd::AssertValid();
  218. }
  219.  
  220. void CMainFrame::Dump(CDumpContext& dc) const
  221. {
  222.     CFrameWnd::Dump(dc);
  223. }
  224.  
  225. #endif //_DEBUG
  226.  
  227. /////////////////////////////////////////////////////////////////////////////
  228. // CMainFrame message handlers
  229.  
  230.  
  231. BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
  232. {
  233.     // Set window size/placement to previous setting stored in the registry
  234.     UINT wpSize;
  235.     WINDOWPLACEMENT* pwp;
  236.     if( AfxGetApp()->GetProfileBinary( TEXT("Settings"), TEXT("WindowPlacement"), (BYTE**)&pwp, &wpSize ) )
  237.     {
  238.         SetWindowPlacement( pwp );
  239.         delete[] pwp;
  240.     }
  241.  
  242.     // Create a vertical splitter with 1 row, 2 columns
  243.     if (!m_wndSplitterMain.CreateStatic(this, 1, 2))
  244.     {
  245.         TRACE0("Failed to create vertical splitter\n");
  246.         return FALSE;
  247.     }
  248.  
  249.     // Add the left splitter pane - which is a nested splitter with 2 rows
  250.     if (!m_wndSplitterLeft.CreateStatic(
  251.         &m_wndSplitterMain,     // our parent window is the main vertical splitter
  252.         2, 1,               // the new splitter is 2 rows, 1 column
  253.         WS_CHILD | WS_VISIBLE,
  254.         m_wndSplitterMain.IdFromRowCol(0, 0)
  255.        ))
  256.     {
  257.         TRACE0("Failed to create left splitter\n");
  258.         return FALSE;
  259.     }
  260.     
  261.     // Add the right splitter pane - which is a nested splitter with 2 rows
  262.     if (!m_wndSplitterRight.CreateStatic(
  263.         &m_wndSplitterMain,     // our parent window is the main vertical splitter
  264.         2, 1,               // the new splitter is 2 rows, 1 column
  265.         WS_CHILD | WS_VISIBLE,
  266.         m_wndSplitterMain.IdFromRowCol(0, 1)
  267.        ))
  268.     {
  269.         TRACE0("Failed to create right splitter\n");
  270.         return FALSE;
  271.     }
  272.  
  273.     m_wndSplitterRight.SetPrev( &m_wndSplitterLeft );
  274.     m_wndSplitterRight.SetNext( &m_wndSplitterLeft );
  275.     
  276.     m_wndSplitterLeft.SetPrev( &m_wndSplitterRight );
  277.     m_wndSplitterLeft.SetNext( &m_wndSplitterRight );
  278.  
  279.     INT cx = AfxGetApp()->GetProfileInt( TEXT("Settings"), TEXT("LeftColumnWidth"), 400 );
  280.     INT cyTopLeft = AfxGetApp()->GetProfileInt( TEXT("Settings"), TEXT("TopLeftPaneHeight"), 500 );
  281.     INT cyTopRight = AfxGetApp()->GetProfileInt( TEXT("Settings"), TEXT("TopRightPaneHeight"), 400 );
  282.  
  283.     m_wndSplitterMain.SetColumnInfo( 0, cx, 50 );
  284.  
  285.     // Top right view: a CRenderView
  286.     if (!m_wndSplitterRight.CreateView(0, 0,
  287.         pContext->m_pNewViewClass, CSize(50, cyTopRight), pContext))
  288.     {
  289.         TRACE0("Failed to create CRenderView pane\n");
  290.         return FALSE;
  291.     }
  292.     
  293.     // Bottom right view: a COptionsView
  294.     if (!m_wndSplitterRight.CreateView(1, 0,
  295.         RUNTIME_CLASS(COptionsView), CSize(50, 50), pContext))
  296.     {
  297.         TRACE0("Failed to create COptionsView pane\n");
  298.         return FALSE;
  299.     }
  300.     
  301.     // Top left view: a CTextView
  302.     if (!m_wndSplitterLeft.CreateView(0, 0,
  303.         RUNTIME_CLASS(CTextView), CSize(50, cyTopLeft), pContext))
  304.     {
  305.         TRACE0("Failed to create CTextView pane\n");
  306.         return FALSE;
  307.     }
  308.     
  309.     // Bottom left view: a CErrorsView
  310.     if (!m_wndSplitterLeft.CreateView(1, 0,
  311.         RUNTIME_CLASS(CErrorsView), CSize(50, 50), pContext))
  312.     {
  313.         TRACE0("Failed to create CErrorsView pane\n");
  314.         return FALSE;
  315.     }
  316.     return TRUE;
  317. }
  318.  
  319. void CMainFrame::OnRender() 
  320. {
  321.     CRenderView* pRenderView = (CRenderView*)m_wndSplitterRight.GetPane(0, 0);
  322.     pRenderView->SendMessage(WM_COMMAND, ID_VIEW_RENDER);
  323. }
  324.  
  325. void CMainFrame::ActivateTextView()
  326. {
  327.     m_wndSplitterLeft.GetPane(0, 0)->SetActiveWindow();
  328. }
  329.  
  330. void CMainFrame::ActivateErrorsView()
  331. {
  332.     m_wndSplitterLeft.GetPane(1, 0)->SetActiveWindow();
  333. }
  334.  
  335. void CMainFrame::ActivateOptionsView()
  336. {
  337.     m_wndSplitterRight.GetPane(1, 0)->SetActiveWindow();
  338. }
  339.  
  340. void CMainFrame::TextViewUpdateFont()
  341. {
  342.     CTextView* pTextView = (CTextView*)m_wndSplitterLeft.GetPane(0, 0);
  343.     if( pTextView != NULL )
  344.         pTextView->UpdateFont();
  345. }
  346.  
  347.  
  348. void CMainFrame::OnViewChangeDevice() 
  349. {
  350.     CRenderView* pRenderView = (CRenderView*)m_wndSplitterRight.GetPane(0, 0);
  351.     pRenderView->ChangeDevice();
  352. }
  353.  
  354. void CMainFrame::SelectLine(int iLine)
  355. {
  356.     CTextView* pTextView = (CTextView*)m_wndSplitterLeft.GetPane(0, 0);
  357.     pTextView->SelectLine( iLine );
  358. }
  359.  
  360. void CMainFrame::OnSize(UINT nType, int cx, int cy) 
  361. {
  362.     CFrameWnd::OnSize(nType, cx, cy);
  363.     
  364.     // TODO: Add your message handler code here
  365.     m_wndSplitterLeft.PreserveLastPaneSize();
  366.     m_wndSplitterRight.PreserveLastPaneSize();
  367. }
  368.  
  369. void CMainFrame::OnClose() 
  370. {
  371.     WINDOWPLACEMENT wp;
  372.     GetWindowPlacement( &wp );
  373.     AfxGetApp()->WriteProfileBinary( TEXT("Settings"), TEXT("WindowPlacement"), (BYTE*)&wp, wp.length );
  374.  
  375.     INT cxCur;
  376.     INT cxMin;
  377.     m_wndSplitterMain.GetColumnInfo( 0, cxCur, cxMin );
  378.  
  379.     AfxGetApp()->WriteProfileInt( TEXT("Settings"), TEXT("LeftColumnWidth"), cxCur );
  380.  
  381.     INT cyCur;
  382.     INT cyMin;
  383.     m_wndSplitterLeft.GetRowInfo( 0, cyCur, cyMin );
  384.     AfxGetApp()->WriteProfileInt( TEXT("Settings"), TEXT("TopLeftPaneHeight"), cyCur );
  385.  
  386.     m_wndSplitterRight.GetRowInfo( 0, cyCur, cyMin );
  387.     AfxGetApp()->WriteProfileInt( TEXT("Settings"), TEXT("TopRightPaneHeight"), cyCur );
  388.  
  389.     CFrameWnd::OnClose();
  390. }
  391.  
  392. void CMainFrame::OnActivateApp(BOOL bActive, DWORD dwThreadID)
  393. {
  394.     ((CEffectEditApp*)AfxGetApp())->SetAppActivated(bActive ? true : false);
  395. }
  396.  
  397. void CMainFrame::SetRowCol(int iRow, int iCol)
  398. {
  399.     TCHAR tszBuf[20];
  400.  
  401.     _stprintf( tszBuf, _T("Ln %d"), iRow );
  402.     m_wndStatusBar.SetPaneText( 1, tszBuf );
  403.     _stprintf( tszBuf, _T("Col %d"), iCol );
  404.     m_wndStatusBar.SetPaneText( 2, tszBuf );
  405. }
  406.